<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\Page as PageModel;
use app\common\model\jia\BuildFloor as FloorModel;
use think\facade\Db;
use think\response\Json;

/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{
    /**
     * 页面数据
     * @param int|null $pageId 页面ID, 不传的话默认为首页
     * @return Json
     * @throws \cores\exception\BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $pageId = null): Json
    {
        // $order = Db::name('order')->select()->toArray();
        // foreach ($order as $key => $value) {
        // 	if ($value['hour_type'] == 10) {
        // 		$time = (int)$value['peihu_time'] + 12 * 60 * 60;
        // 		$start = Date('H:i', (int)$value['peihu_time']);
        // 		$end = Date('H:i', $time);
        // 		$dd = $start . '-' . $end;
        // 	} else {
        // 		$time = (int)$value['peihu_time'] + 86400;
        // 		$start = Date('H:i', (int)$value['peihu_time']);
        // 		$end = Date('H:i', $time);
        // 		$dd = $start . '-' . $end;
        // 	}
        // 	Db::name('order')->where('order_id', $value['order_id'])->update(['peihu_time' => $dd]);
        // }
        // 页面数据
        $model    = new PageModel;
        $pageData = $model->getPageData($pageId);
        return $this->renderSuccess(compact('pageData'));
    }
  
    //楼栋
    public function getBuildList(int $shopId = 0): Json
    {
        $buildList = Db::name('jia_build')
            ->field('name as label,build_id as value,floor')->where('shop_id', $shopId)->where('is_delete', 0)->select()->toArray();

        foreach ($buildList as $key => $value) {
            $list = [];
            for ($i = 1; $i <= $value['floor']; $i++) {

                $list[] = ['label' => $i . '层', 'value' => $i];
            }
            $buildList[$key]['children'] = $list;
        }

        return $this->renderSuccess(compact('buildList'));
    }
    public function getFloorList(int $buildId = 0): Json
    {

        $list = [];
        // for ($i = 1; $i <= $floor; $i++) {

        // 	$list[] = ['name' => $i . '层', 'num' => $i];
        // }
        $model = new FloorModel;

        $list    = $model->getListAll(['buildId' => $buildId]);
        $minDate = Date('Y-m-d');
        return $this->renderSuccess(compact('list', 'minDate'));
    }
    public function getHouseList(int $roomId = 0): Json
    {

        $list = Db::name('jia_room_house')->where('room_id', $roomId)
        ->where('is_delete',0)
        ->field('name as label,house_id as value')->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    public function getEndTime($start, $type = 10): Json
    {

        $start = strtotime('2024-06-04 ' . $start);
        if ($type == 10) {
            $end = $start + 60 * 60 * 12;
        } else {
            $end = $start + 60 * 60 * 24;
        }

        $time = Date('H:i', $start) . '至' . Date('H:i', $end);

        return $this->renderSuccess(compact('time'));
    }

    //获取停车总时长
    public function getParkTime($start, $end = ''): Json
    {

        $price = 2.5; //半小时费用
                      //$price =0.01; //半小时费用

        $endTime = $end == '' ? Date('Y-m-d H:i') : $end;

        $start = strtotime($start);
        $end   = $end == '' ? time() : strtotime($end);
        $sub   = $end - $start;

        $hours   = $sub / 3600;
        $seconds = $sub % 3600;

        $minute = round($seconds / 60);

        $hour = floor($hours);
        if ($sub <= 3600 * 3) {

            $fee = 0;
        } else {

            //计算超时费用

            $hour_sub = $hour - 3;

            $fee = $hour_sub * $price * 2;

            $min = ceil($minute / 30);

            $fee = $fee + $min * $price;

        }

        $time = $hour . '小时' . $minute . '分钟';

        return $this->renderSuccess(compact('fee', 'time', 'endTime'));
    }
    public function getBedList(): Json
    {
        $list1 = [];
        for ($i = 1; $i <= 30; $i++) {

            $list1[] = $i;
        }
        $list2 = [];
        for ($i = 31; $i <= 200; $i++) {

            $list2[] = $i;
        }

        return $this->renderSuccess(compact('list1', 'list2'));
    }
    //订餐日期
    public function getDate($date = ''): Json
    {
        if ($date == '') {
            $time = time() + 86400;
            $date = Date('Y-m-d', $time);
        }
        $time     = strtotime($date);
        $today    = strtotime(Date('Y-m-d'));
        $dateNext = Date('Y-m-d', $time + 86400);
        $nextTime = $time - 86400;
        if ($nextTime <= $today) {
            $dateLast = '';
        } else {
            $dateLast = Date('Y-m-d', $nextTime);
        }

        $week = $this->get_week_day($time);

        return $this->renderSuccess(compact('date', 'week', 'dateLast', 'dateNext'));
    }

    public function get_week_day($time)
    {

        $week_array = ["日", "一", "二", "三", "四", "五", "六"];

        $week = date("w", $time);

        return "星期" . $week_array[$week];

    }

    public function getDay($start, $end, $type = 10): Json
    {

        $start = strtotime($start);
        $end   = strtotime($end);
        $sub   = $end - $start;
        // if ($type == 10) {
        // 	$day = round($sub / 86400) + 1;
        // } else {
        // 	$day = round($sub / 86400) > 0 ? round($sub / 86400)+1 : 1;
        // }
        $day = round($sub / 86400) + 1;

        return $this->renderSuccess(compact('day'));
    }
    // public function getTimeList($start_time, $end_time): Json {
    // 	$aa = [];
    // 	for ($i = $start_time; $i <= $end_time; $i++) {
    // 		if ($i < 10) {
    // 			$time['name'] = '0' . $i . '时';
    // 			$time['id'] = $i;
    // 		} else {
    // 			$time['name'] = $i . '时';
    // 			$time['id'] = $i;
    // 		}
    // 		$aa[] = $time;
    // 	}

    // 	$bb = [['name' => '00', 'id' => 1], ['name' => '30', 'id' => 2]];
    // 	$list[] = $aa;
    // 	$list[] = $bb;

    // 	return $this->renderSuccess(compact('list'));
    // }

    public function getTimeList($hourType): Json
    {
        $list = Db::name('jia_time')->where('category_id', $hourType)->order('sort asc')->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    public function getParkList(int $shopId = 0): Json
    {
        $list = Db::name('store_shop_park')->order('sort asc')->select()->toArray();

        return $this->renderSuccess(compact('list'));
    }
    //陪检费用，计算
    public function getRunTime($start, $end = ''): Json
    {

        //超时十分钟，15元
        $chao_price  = 15;
        $categoryIds = Db::name('goods_category_rel')->where('category_id', 10004)->select()->toArray();
        $categoryIds = array_column($categoryIds, 'goods_id');

        $endTime = $end == '' ? Date('Y-m-d H:i') : $end;

        $start = strtotime($start);
        $end   = $end == '' ? time() : strtotime($end);
        $sub   = $end - $start;
        $hours = $sub / 3600;

        $hour = floor($hours);

        $minute = $sub % 3600;
        $minute = round($minute / 60);

        if ($hour < 1) {

            $fee = 0;
        } else {
            $fee = $hour * 30;
            if (10 < $minute && $minute <= 40) {
                $fee += 15;
            }
            if ($minute > 40) {
                $fee += 30;
            }

        }

        $time = $hour . '小时' . $minute . '分钟';

        return $this->renderSuccess(compact('fee', 'time', 'endTime'));
    }
}
